package com.huhao.wshare;

import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import android.view.View;
import android.widget.RadioGroup;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.huhao.wshare.Dynamic.DynamicFragment;
import com.huhao.wshare.Friends.FriendsFragment;
import com.huhao.wshare.Profile.ProfileFragment;

public class MainActivity extends BaseActivity {

    private Fragment feedFragment;
    private Fragment friendsFragment;
    private Fragment profileFragment;
    private Fragment currentFragment;
    private RadioGroup bottomNavigation;
    private SwipeRefreshLayout swipeRefreshLayout;
    private int lastCheckedId = R.id.btn_profile;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bottomNavigation = findViewById(R.id.bottom_navigation);
        swipeRefreshLayout = findViewById(R.id.main_swipeRefreshLayout);

        // 初始化 Fragment
        feedFragment = new DynamicFragment();
        friendsFragment = new FriendsFragment();
        profileFragment = new ProfileFragment();

        // 默认显示个人页面
        showFragment(profileFragment, false);
        bottomNavigation.check(R.id.btn_profile);

        // 导航栏点击切换 Fragment
        bottomNavigation.setOnCheckedChangeListener((group, checkedId) -> {
            boolean isRightToLeft = getNavigationDirection(lastCheckedId, checkedId);
            if (checkedId == R.id.btn_feed) {
                showFragment(feedFragment, isRightToLeft);
            } else if (checkedId == R.id.btn_friends) {
                showFragment(friendsFragment, isRightToLeft);
            } else if (checkedId == R.id.btn_profile) {
                showFragment(profileFragment, isRightToLeft);
            }
            lastCheckedId = checkedId;
        });

        // 设置下拉刷新监听
        swipeRefreshLayout.setOnRefreshListener(() -> {
            // 处理下拉刷新逻辑，根据当前显示的 Fragment 执行对应刷新操作
            if (currentFragment instanceof Refreshable) {
                ((Refreshable) currentFragment).refresh();
            }
            swipeRefreshLayout.setRefreshing(false);
        });
    }

    private void showFragment(Fragment fragment, boolean isRightToLeft) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (currentFragment != null) {
            if (isRightToLeft) {
                transaction.setCustomAnimations(
                        R.anim.slide_in_right,
                        R.anim.slide_out_left,
                        R.anim.slide_in_left,
                        R.anim.slide_out_right
                );
            } else {
                transaction.setCustomAnimations(
                        R.anim.slide_in_left,
                        R.anim.slide_out_right,
                        R.anim.slide_in_right,
                        R.anim.slide_out_left
                );
            }
            transaction.hide(currentFragment);
        }
        if (!fragment.isAdded()) {
            transaction.add(R.id.fragment_container, fragment);
        }
        transaction.show(fragment);
        transaction.commit();
        currentFragment = fragment;
    }

    private boolean getNavigationDirection(int lastCheckedId, int newCheckedId) {
        return newCheckedId > lastCheckedId;
    }

    /**
     * 隐藏底部导航栏
     */
    public void hideBottomNavigation() {
        bottomNavigation.setVisibility(View.GONE);
    }

    /**
     * 显示底部导航栏
     */
    public void showBottomNavigation() {
        bottomNavigation.setVisibility(View.VISIBLE);
    }

    public void setSwipeRefreshEnabled(boolean enabled) {
        swipeRefreshLayout.setEnabled(enabled);
    }
}